package lcof;

import java.util.ArrayList;
import java.util.List;

public class FullJustify68 {

    public static void main(String[] args) {
        List<String> strings = new FullJustify68().fullJustify(new String[]{"What","must","be","acknowledgment","shall","be"}, 16);
        System.out.println(strings);
    }

    public List<String> fullJustify(String[] words, int maxWidth) {
        List<List<String>> total = new ArrayList<>();
        List<String> current = new ArrayList<>();
        total.add(current);
        for (String word : words) {
            if (canPut(current, maxWidth, word)) {
                current.add(word);
            } else {
                current = new ArrayList<>();
                total.add(current);
                current.add(word);
            }
        }
        return handleList(total, maxWidth);
    }


    /**
     * 当前行内能否添加单词
     */
    public boolean canPut(List<String> currentList, int maxWidth, String word) {
        int charLen = 0;
        for (String s : currentList) {
            charLen += s.length() + 1;
        }
        if (charLen + word.length() <= maxWidth) {
            return true;
        }
        return false;
    }

    public static String repeatString(String str, int times) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < times; i++) {
            sb.append(str);
        }
        return sb.toString();
    }


    public List<String> handleList(List<List<String>> total, int maxWidth) {
        List<String> result = new ArrayList<>();
        for (int i = 0; i < total.size(); i++) {
            StringBuilder sb = new StringBuilder();
            List<String> current = total.get(i);
            if (i < total.size() - 1) {
                //常规处理
                if (current.size() == 1) {
                    sb.append(current.get(0));
                    sb.append(repeatString(" ", maxWidth - sb.length()));
                    result.add(sb.toString());
                    continue;
                }
                int textLen = current.stream().mapToInt(String::length).sum();
                int allSpace = maxWidth - textLen;
                int space = allSpace / (current.size() - 1);
                String appendSpace = repeatString(" ", space);
                int extra = allSpace % (current.size() - 1);
                for (int j = 0; j < current.size(); j++) {
                    sb.append(current.get(j));
                    if (j < current.size() - 1) {
                        sb.append(appendSpace);
                    }
                    if (extra > 0) {
                        sb.append(" ");
                        extra--;
                    }
                }
            } else {
                //最后一行处理
                for (int j = 0; j < current.size(); j++) {
                    sb.append(current.get(j));
                    if (j < current.size() - 1) {
                        sb.append(" ");
                    }
                }
                //补全所有空格
                sb.append(repeatString(" ", maxWidth - sb.length()));
            }
            result.add(sb.toString());
        }
        return result;
    }
}
